<?php
function send() {
    $arr = $_POST['arr'];
    // print_r(json_decode($arr));
    $data = json_decode($arr, true);

    $username = $data[0]['username'];

    $link = mysqli_connect('127.0.0.1', 'root', 'root', 'demo1');

    $res = mysqli_query($link, "DELETE FROM `shop` WHERE `username`='$username'");

    foreach ($data as $value) {
        // print_r($value);
        $uname = $value['username'];
        $title = $value['title'];
        $price_info = $value['price_info'];
        $img = $value['img'];
        $num = $value['num'];
        $query = mysqli_query($link, "INSERT INTO `shop` (`username`, `title`, `price_info`, `img`, `num`) VALUES('$uname', '$title', '$price_info', '$img', '$num')");
    }

    mysqli_close($link);

    if ($res) {
        $arr = array("message" => "成功",
        "code" => 1,
        );
    } else {
        $arr = array("message" => "失败", "code" => 0);
    }
    print_r(json_encode($arr));
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    send();
}